package com.doupi.partner.service.impl;

import com.doupi.partner.dal.AdministratorMapper;
import com.doupi.partner.dal.model.Administrator;
import com.doupi.partner.dal.model.AdministratorHasRole;
import com.doupi.partner.service.AdministratorHasRoleService;
import com.doupi.partner.service.AdministratorService;
import com.doupi.partner.service.dto.AdministratorDto;
import com.doupi.partner.util.Pagination;
import com.doupi.partner.util.RuntimeBusinessException;
import org.apache.commons.codec.binary.StringUtils;
import org.apache.commons.codec.digest.Md5Crypt;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdministratorServiceImpl implements AdministratorService {
	private final AdministratorMapper administratorMapper;

	private final AdministratorHasRoleService administratorHasRoleService;

	@Autowired
	public AdministratorServiceImpl(AdministratorMapper administratorMapper, AdministratorHasRoleService administratorHasRoleService) {
		this.administratorMapper = administratorMapper;
		this.administratorHasRoleService = administratorHasRoleService;
	}

	public int deleteByPrimaryKey(Long id) {
		int flag = administratorMapper.deleteByPrimaryKey(id);
		if (flag > 0) {
			// 删除管理员角色对应管理
			administratorHasRoleService.deleteByAdministratorId(id);
		}
		return flag;
	}

	public int insert(Administrator record) {
		return administratorMapper.insert(record);
	}

	public Administrator selectByPrimaryKey(Long id) {
		return administratorMapper.selectByPrimaryKey(id);
	}

	public int updateByPrimaryKeySelective(Administrator record) {
		return administratorMapper.updateByPrimaryKeySelective(record);
	}

	public int countAdministrator(Administrator administrator) {
		return administratorMapper.countAdministrator(administrator);
	}

	public List<Administrator> queryAdministrators(Administrator administrator, RowBounds rowBounds) {
		return administratorMapper.queryAdministrators(administrator, rowBounds);
	}

	public Administrator selectAdministratorForUpdateByPrimaryKey(Long id) {
		return administratorMapper.selectAdministratorForUpdateByPrimaryKey(id);
	}

	public Pagination<Administrator, Administrator> queryAdministrators(Pagination<Administrator, Administrator> pagination) {
		int count = countAdministrator(pagination.getParam());
		pagination.setTotalCount(count);
		if (count > 0) {
			pagination.setRecords(queryAdministrators(pagination.getParam(), new RowBounds(pagination.getOffset(), pagination.getLimit())));
		}
		return pagination;
	}

	@Override
	public Administrator login(String mobile, String password) {
		Administrator administrator = selectByMobile(mobile);
		if (null == administrator) {
			throw new RuntimeException("账户或密码不正确");
		}
		if (!StringUtils.equals(Md5Crypt.md5Crypt(password.getBytes(), password), administrator.getPassword())) {
			throw new RuntimeException("账户或密码不正确");
		}
		return administrator;
	}

	@Override
	public Administrator selectByMobile(String mobile) {
		Administrator administrator = new Administrator();
		administrator.setMobile(mobile);

		List<Administrator> list = queryAdministrators(administrator, new RowBounds(0, 1));
		if (!list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public void saveAdministrator(AdministratorDto administratorDto) {
		// 1.插入或更新管理员信息
		int flag;
		if (null == administratorDto.getId() || administratorDto.getId().intValue() <= 0) {
			flag = insert(administratorDto);
		} else {
			flag = updateByPrimaryKeySelective(administratorDto);
		}

		// 2.插入或者更新成功，先删除之前的角色对应关系
		if (flag > 0) {
			administratorHasRoleService.deleteByAdministratorId(administratorDto.getId());
		}

		// 3.插入或者更新成功，重新插入角色对应关系
		if (flag > 0 && null != administratorDto.getAdministratorHasRoles()) {
			for (AdministratorHasRole administratorRole : administratorDto.getAdministratorHasRoles()) {
				if (administratorRole.getAdministratorRoleId() != null) {
					administratorRole.setAdministratorId(administratorDto.getId());
					administratorHasRoleService.insert(administratorRole);
				}
			}
		}
	}

	@Override
	public int updateAdministratorNameAndMobile(Long id, String name, String mobile) {
		Administrator administrator = selectByMobile(mobile);
		if (null != administrator && !administrator.getId().equals(id)) {
			throw RuntimeBusinessException.createException("手机号已经存在");
		}
		if (null == administrator) {
			administrator = new Administrator();
			administrator.setId(id);
		}
		administrator.setName(name);
		administrator.setMobile(mobile);
		return updateByPrimaryKeySelective(administrator);
	}
}
